﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_QueryFrom_frm_Audit : System.Web.UI.Page
{
    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetQueryDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdQueryResult.CurrentPageIndex;
            int intPageCounts = this.dgrdQueryResult.PageCount;

            //判断翻页方向
            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindQueryDataSet(dst, intPageIndex);
        }
    }

    private void GetAudit()
    {
        string strorderID = this.hidCNo.Value;//获得单据编号

        ClsAuditDAL cls_Audit = new ClsAuditDAL();
        DataSet dst = cls_Audit.GetAudits(strorderID);

        if (dst != null)
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();

            cls_Property.SetOrGetQueryDataSet = dst;

            this.BindQueryDataSet(dst, 0);
        }
        else
        {
            this.lblQueryRsCounts.Text = "共0条记录";
            this.lblQueryPageCounts.Text = "共0页";
            this.lblQueryCurrPage.Text = "当前第0页";
            this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = false;
            this.lblQueryRsCounts.ForeColor = this.lblQueryPageCounts.ForeColor = this.lblQueryCurrPage.ForeColor = Color.FromArgb(172, 168, 153);
        }
    }

    private void BindQueryDataSet(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdQueryResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdQueryResult.DataSource = dst;
            this.dgrdQueryResult.DataBind();

            int intPageCount = this.dgrdQueryResult.PageCount;
            int intCurrPage = this.dgrdQueryResult.CurrentPageIndex + 1;
            this.lblQueryRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblQueryPageCounts.Text = "共" + intPageCount + "页";
            this.lblQueryCurrPage.Text = "当前第" + intCurrPage + "页";
            this.lblQueryRsCounts.Visible = this.lblQueryPageCounts.Visible = this.lblQueryCurrPage.Visible = true;
            this.lbtnQueryFirst.Visible = this.lbtnQueryPrev.Visible = this.lbtnQueryNext.Visible = this.lbtnQueryEnd.Visible = true;
            this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = true;
            this.lblQueryRsCounts.ForeColor = this.lblQueryPageCounts.ForeColor = this.lblQueryCurrPage.ForeColor = Color.FromArgb(78, 78, 78);

            if (intPageCount == 1)
            {
                this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = false;
            }
            else if (intCurrPage == intPageCount)
            {
                this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = false;
            }
            else if (intCurrPage - 1 == 0)
            {
                this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = false;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                string strCNo = this.Page.Request.Params["C_No"];//获得单据编号
                string strCType = this.Page.Request.Params["PageID"];//获得单据类型

                this.hidCNo.Value = strCNo;
                this.hidCType.Value = strCType;

                this.GetAudit();//显示批语
            }
            else
            {
                this.lblJs.Text = "";
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        this.GetAudit();
    }

    public void imgSubmit_Click(object sender, ImageClickEventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsAuditDAL cls_AuditDAL = new ClsAuditDAL();

            string strCNo = this.hidCNo.Value;//单据编号
            int intCType = Int32.Parse(this.hidCType.Value);//单据类型
            string strCurrUser = (this.hidCurrUserName.Value == "") ? strUserID : this.hidCurrUserName.Value;//确认人

            //确认流控制
            string strCurrUserStation = cls_AuditDAL.GetUserAuditStation(strCurrUser, intCType);//获取当前登录用户在确认设置中对应的岗位

            if (strCurrUserStation != "")
            {
                string strNoAuditFirstStation = cls_AuditDAL.GetOrderNoAuditFirstStation(strCNo, intCType);//获取该单据还未确认的第一个岗位
                string strEndAuditStation = cls_AuditDAL.GetEndAuditStation(intCType);//获取该单据终审岗位

                //到当前岗位的用户确认
                if (strCurrUserStation == strNoAuditFirstStation)
                {
                    bool bolStat = (this.dropKeyName.SelectedValue == "0") ? true : false;//同意或者拒绝
                    string strRemark = this.txtKeyValue.Text;//确认批语

                    Boolean bolIsSussee = cls_AuditDAL.AddAudit(strCNo, strCurrUser, bolStat, strRemark);//添加确认记录

                    if (bolIsSussee)
                    {
                        this.GetAudit();//重新绑定

                        if (strNoAuditFirstStation == strEndAuditStation && bolStat)
                        {
                            //cls_AuditDAL.UpdateAutdiStat(strCNo, intCType, strCurrUser);
                            //----------------------BEGIN KIDY----------------------------------------
                            string strReturn = cls_AuditDAL.UpdateAutdiStat(strCNo, -1, strCurrUser);
                            string[] arrReValue = strReturn.Split(new char[] { '|' });
                            if (arrReValue[0] == "N")//不可以取消确认，得到返回的批号
                            {
                                if (arrReValue[1].Length > 0 && arrReValue[2].Length > 0)
                                {
                                    this.labMessage.Text = "<script language='javascript'>alert('当前" + arrReValue[1] + "商品" + arrReValue[2] + "批号未入库,请确认！');</script>";
                                }
                                else
                                {
                                    this.labMessage.Text = "<script language='javascript'>alert('当前" + arrReValue[1] + "商品未入库,请确认！');</script>";
                                }
                            }
                            else if (arrReValue[0] == "Q")
                            {
                                if (arrReValue[1].Length > 0 && arrReValue[2].Length > 0)
                                {
                                    this.labMessage.Text = "<script language='javascript'>alert('当前" + arrReValue[1] + "商品" + arrReValue[2] + "批号库存不足,请调整！');</script>";
                                }
                                else
                                {
                                    this.labMessage.Text = "<script language='javascript'>alert('当前" + arrReValue[1] + "商品库存不足,请确认！');</script>";
                                }

                            }
                            else
                            {
                                if (strReturn != "0")
                                {
                                    Response.Write("<script>alert('确认失败！');</script>");
                                }
                            }
                            //----------------------END KIDY----------------------------------------
                        }
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('确认失败，请重试！')</script>";
                    }
                }
                else
                {
                    this.lblJs.Text = "<script>alert('待审岗位不匹配，确认无效！')</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('您无权确认此单据！')</script>";
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void dgrdQueryResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            bool state = bool.Parse(e.Item.Cells[1].Text.Trim());
            TextBox txt2 = (TextBox)e.Item.FindControl("txt2");

            txt2.Text = (!state) ? "拒绝" : "同意";
        }
    }

    protected void ibtnAdvLogin_Click(object sender, ImageClickEventArgs e)
    {
        string strUserID = this.UserID.Text.Trim();
        string strUserPwd = this.UserPwd.Text.Trim();

        if (strUserID != "")
        {
            if (strUserPwd != "")
            {
                ClsAuditDAL cls_Audit = new ClsAuditDAL();
                ClsUserLoginDAL cls_UserLoginDAL = new ClsUserLoginDAL();

                int bolIsSussee = cls_UserLoginDAL.SysUserLogin(strUserID, strUserPwd, "", "", 1);//登陆

                if (bolIsSussee > 0)//登陆成功
                {
                    this.hidCurrUserName.Value = strUserID;
                    this.lblJs.Text = "<script>alert('高级登录成功！')</script>";
                }
                else
                {
                    this.lblJs.Text = "<script>alert('用户名或密码错误！')</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('密码不能为空！')</script>";
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('用户名不能为空！')</script>";
        }
    }

    protected void ibtnAdvSearch_Click(object sender, ImageClickEventArgs e)
    {
        this.lblJs.Text = "<script>alert('暂无此功能')</script>";
    }

    protected void ibtnClacel_Click(object sender, ImageClickEventArgs e)
    {
        this.lblJs.Text = "<script>window.returnValue='True';window.close();</script>";
    }
}
